<template>
  <uploadExcel :on-success="handleSuccess" :before-upload="beforeUpload" />
</template>

<script>
import { formatExcelDate } from '@/utils'
import { leadEmployees } from '@/api/employee'
// 中文转英文
// const mapInfo = {
//             '入职日期': 'timeOfEntry',
//             '手机号': 'mobile',
//             '姓名': 'username',
//             '转正日期': 'correctionTime',
//             '工号': 'workNumber',
//             '部门': 'departmentName',

//         }

// 英文转中文
const mapInfo = {
  '入职日期': 'timeOfEntry',
  '手机号': 'mobile',
  '姓名': 'username',
  '转正日期': 'correctionTime',
  '工号': 'workNumber',
  '部门': 'departmentName'

}
const zhTranslateEn = (data) => {
  const results = data.map(item => {
    const enKeyObj = {}
    const zhKeys = Object.keys(item) // ['姓名', '手机号', '入职日期', '转正日期', '工号', '部门']
    zhKeys.forEach(zhKey => {
      // 把中文键名换成英文键名
      const enKey = mapInfo[zhKey]
      // 对象键名等于键值
      // enKeyObj[enKey] = item[zhKey]
      // 调用转化时间
      if (enKey === 'timeOfEntry' || enKey === 'correctionTime') {
        // 转换成标准时间
        enKeyObj[enKey] = formatExcelDate(item[zhKey])
      } else {
        enKeyObj[enKey] = item[zhKey]
      }
    })

    return enKeyObj
  })
  return results
}
export default {
  data() {
    return {
      tableData: [],
      tableHeader: []
    }
  },
  methods: {
    beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1

      if (isLt1M) {
        return true
      }

      this.$message({
        message: 'Please do not upload files larger than 1m in size.',
        type: 'warning'
      })
      return false
    },
    handleSuccess({ results, header }) {
      this.tableData = results
      this.tableHeader = header
      console.log(results, 'results')
      console.log(header, 'header')
      // 转换数据
      const data = zhTranslateEn(results)
      console.log(zhTranslateEn(results), 'zhTranslateEn(results)')
      this.leadMoreEmployees(data)
    },
    // 发送请求,批量添加
    async leadMoreEmployees(data) {
      try {
        const res = await leadEmployees(data)
        console.log('leadEmployees', res)
        this.$router.back()
      } catch (err) {
        console.log('leadEmployees', err)
      }
    }

  }
}
</script>

<style>

</style>
